Jackson ব্যবহার করে JSON ফিল্ডের নাম এবং জাভার ফিল্ডের নামের মধ্যে মেপিং করতে কাস্টম নামকরণের কনভেনশন প্রয়োগ করা যায়।
1. Custom Naming Strategy
Jackson PropertyNamingStrategy এবং JacksonAnnotationIntrospector ব্যবহার করে JSON ফিল্ডের নাম কাস্টমাইজ করতে সহায়তা করে।
উদাহরণ: Snake Case কনভেনশন
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
class User {
private String firstName;
private String lastName;
// Constructors, Getters, and Setters
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
public class CustomNamingStrategyExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// Snake Case Naming Strategy
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
User user = new User("John", "Doe");
// Serialize
String json = mapper.writeValueAsString(user);
System.out.println("Serialized JSON: " + json);
// Deserialize
String userJson = "{\"first_name\":\"John\",\"last_name\":\"Doe\"}";
User deserializedUser = mapper.readValue(userJson, User.class);
System.out.println("Deserialized User: " + deserializedUser.getFirstName() + " " + deserializedUser.getLastName());
}
}
আউটপুট:
Serialized JSON: {"first_name":"John","last_name":"Doe"}
Deserialized User: John Doe
2. @JsonProperty দিয়ে Custom Name নির্ধারণ করা
@JsonProperty ব্যবহার করে নির্দিষ্ট ফিল্ডের নাম কাস্টমাইজ করা যায়।
উদাহরণ: @JsonProperty ব্যবহার
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
class Employee {
@JsonProperty("employee_id")
private int id;
@JsonProperty("employee_name")
private String name;
// Constructors, Getters, and Setters
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class JsonPropertyExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
Employee employee = new Employee(1, "Alice");
// Serialize
String json = mapper.writeValueAsString(employee);
System.out.println("Serialized JSON: " + json);
// Deserialize
String employeeJson = "{\"employee_id\":1,\"employee_name\":\"Alice\"}";
Employee deserializedEmployee = mapper.readValue(employeeJson, Employee.class);
System.out.println("Deserialized Employee: " + deserializedEmployee.getName());
}
}
আউটপুট:
Serialized JSON: {"employee_id":1,"employee_name":"Alice"}
Deserialized Employee: Alice
3. Custom Naming Strategy ক্লাস তৈরি করা
Jackson-এ কাস্টম নামকরণ লজিক প্রয়োগ করতে একটি কাস্টম PropertyNamingStrategy তৈরি করা যায়।
উদাহরণ: Custom Naming Strategy
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.ObjectMapper;
class CustomNamingStrategy extends PropertyNamingStrategies.PropertyNamingStrategyBase {
@Override
public String translate(String propertyName) {
// Custom naming logic: Prefix 'my_' to all properties
return "my_" + propertyName;
}
}
class Product {
private String name;
private double price;
// Constructors, Getters, and Setters
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
public class CustomNamingStrategyExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// Custom Naming Strategy
mapper.setPropertyNamingStrategy(new CustomNamingStrategy());
Product product = new Product("Laptop", 1500.0);
// Serialize
String json = mapper.writeValueAsString(product);
System.out.println("Serialized JSON: " + json);
// Deserialize
String productJson = "{\"my_name\":\"Laptop\",\"my_price\":1500.0}";
Product deserializedProduct = mapper.readValue(productJson, Product.class);
System.out.println("Deserialized Product: " + deserializedProduct.getName() + " - $" + deserializedProduct.getPrice());
}
}
আউটপুট:
Serialized JSON: {"my_name":"Laptop","my_price":1500.0}
Deserialized Product: Laptop - $1500.0
4. Default Naming Strategies
Jackson ইতোমধ্যে বেশ কিছু ডিফল্ট Naming Strategies প্রদান করে:
SNAKE_CASE:firstName→first_nameUPPER_CAMEL_CASE:firstName→FirstNameLOWER_CAMEL_CASE:firstName→firstName(ডিফল্ট)KEBAB_CASE:firstName→first-name
Naming Strategy সেট করা
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.KEBAB_CASE);
5. টিপস
- Custom Logic প্রয়োজন হলে: কাস্টম
PropertyNamingStrategyব্যবহার করুন। - Annotation প্রাধান্য:
@JsonPropertyব্যবহার করলে সেটি কাস্টম Naming Strategy এর উপর প্রাধান্য পায়। - Global Naming Strategy: পুরো অ্যাপ্লিকেশনে একধরনের নামকরণ প্রয়োগ করতে
ObjectMapper-এ Naming Strategy সেট করুন।
6. Use Cases
- API Integration: ভিন্ন কনভেনশনের API-এর সাথে কাজ করার জন্য।
- Legacy Systems: পুরানো ডেটা ফরম্যাট বা কনভেনশন বজায় রাখতে।
- Custom Requirements: স্পেসিফিক ডেটা ফরম্যাট কাস্টমাইজ করতে।
Jackson এর Naming Strategies এবং কাস্টম লজিক JSON ফিল্ড এবং জাভা প্রোপার্টির মধ্যে কাস্টমাইজড ম্যাপিং করতে কার্যকর। ছোট ফিল্ড কনফিগারেশনের জন্য @JsonProperty এবং বড় বা গ্লোবাল প্রয়োজনে Naming Strategy ব্যবহার করা উপযুক্ত।
Read more